package com.oscar.ehome.view.setview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.view.setview.SigninActivity;

import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<Map<String, Object>> mDataList = new ArrayList<Map<String, Object>>();
    private Map<String, Object> map = new HashMap<String, Object>();

    public ImageGridAdapter(Context context, List<Map<String, Object>> mDataList) {
        this.mContext = context;
        this.mDataList = mDataList;
    }

    public void setLists(List<Map<String, Object>> mDataList) {
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder mHolder;

        if (convertView == null) {
            convertView = View
                    .inflate(mContext, R.layout.item_image_list, null);
            mHolder = new ViewHolder();
            mHolder.imageIv = (ImageView) convertView.findViewById(R.id.image);
            mHolder.selectedIv = (ImageView) convertView
                    .findViewById(R.id.selected_tag);
            mHolder.selectedBgTv = (TextView) convertView
                    .findViewById(R.id.image_selected_bg);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        try {

        }catch (OutOfMemoryError error){}
        map = mDataList.get(position);
        String path = MapUtils.getString(map, "imagePath");

        //使用谷歌官方提供的Glide加载图片
//        GildeUtils.imgLoadPic(mContext, path, mHolder.imageIv, "nat");

        GildeUtils.loadUrlImages(path, mHolder.imageIv, mContext);

        if (MapUtils.getBoolean(map, "isSelect")) {
            mHolder.selectedIv.setImageDrawable(mContext.getResources()
                    .getDrawable(R.drawable.tag_selected));
            mHolder.selectedIv.setVisibility(View.VISIBLE);
            mHolder.selectedBgTv
                    .setBackgroundResource(R.drawable.image_selected);
        } else {
            mHolder.selectedIv.setImageDrawable(null);
            mHolder.selectedIv.setVisibility(View.GONE);
        }

        return convertView;
    }

   public static class ViewHolder {
        private ImageView imageIv;
        private ImageView selectedIv;
        private TextView selectedBgTv;
    }

}
